package day1;

import java.util.ArrayList;

public class Day1_02 {

    public ArrayList<Integer> intersection (ArrayList<Integer> nums1, ArrayList<Integer> nums2) {
        //if(nums1 == null || nums2 == null) return null;
        // write code here
        ArrayList<Integer> ret = new ArrayList<Integer>();
        for(int i = 0; i < nums1.size(); i++) {
            Integer cur = nums1.get(i);
            for( int j = 0; j < nums2.size(); j++) {
                if(cur.equals( nums2.get(j) )) {
                    if(!ret.contains(cur)) ret.add(cur);
                    break;
                }
            }
        }
        return ret;
    }
    public ArrayList<Integer> intersection2 (ArrayList<Integer> nums1, ArrayList<Integer> nums2) {
        boolean[] hash = new boolean[1010];
        ArrayList<Integer> ret = new ArrayList<Integer>();
        for(int i = 0; i < nums1.size(); i++) {
            hash[nums1.get(i).intValue()] = true;
        }
        for(int i = 0; i < nums2.size(); i++) {
            if(hash[nums2.get(i).intValue()]) {
                ret.add(nums2.get(i));
                hash[nums2.get(i).intValue()] = false;
            }
        }
        return ret;
    }

}
//https://www.nowcoder.com/practice/56ea71d1f4e94de2aaec10e985874cce?tpId=196&tqId=40269&ru=/exam/oj